package logic

import (
	"context"
	"gorm.io/gorm"
	"taobao-merchandise/model/mysql"

	"taobao-merchandise/taobaorpc/internal/svc"
	"taobao-merchandise/taobaorpc/taobaorpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateLogic {
	return &UpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdateLogic) Update(in *taobaorpc.UpdateRequest) (*taobaorpc.UpdateResponse, error) {
	// todo: add your logic here and delete this line

	product := mysql.Product{
		Model:  gorm.Model{ID: uint(in.ProductId)},
		Title:  in.Title,
		UserId: int(in.UserId),
		Price:  float64(in.Price),
		Stock:  int(in.Stock),
		Image:  in.Image,
	}
	mysql.Db.Debug().Updates(&product)

	return &taobaorpc.UpdateResponse{
		ProductId: int64(product.ID),
	}, nil
}
